<template>
  <page-header-wrapper>
    <a-card :border="false">
      <div style="margin: 0 0 15px; border: 1px solid #91d5ff; padding: 8px 15px; background-color: #e6f7ff">
        回调接口地址（请在“应用列表->应用详情->回调地址设置”中进行设置）
        <span
          @click="onCopyText(callbackLink)"
          style="color: #448ef7;cursor: pointer;"
        >
          {{ callbackLink }}</span
        >
      </div>

      <a-form-model ref="ruleForm" :model="form" :rules="rules" :label-col="{ span: 3 }" :wrapper-col="{ span: 8 }">
        <a-form-model-item label="开放平台地址">
          <a style="margin-left: 10px" href="https://openweb.jushuitan.com/user/login" target="_blank"
            >https://openweb.jushuitan.com/user/login</a
          >
        </a-form-model-item>
        <a-form-model-item label="app_Key" prop="app_Key">
          <a-input v-model="form.app_Key" placeholder="请输入app_Key" />
        </a-form-model-item>

        <a-form-model-item label="app_Secret" prop="app_Secret">
          <a-input v-model="form.app_Secret" placeholder="请输入app_Secret" />
        </a-form-model-item>

        <a-form-model-item :wrapper-col="{ span: 14, offset: 4 }">
          <a-button type="primary" @click="submitForm" :loading="modalLoading"> 提交 </a-button>
          <a-button style="margin-left: 10px" @click="$router.back(-1)"> 取消 </a-button>
        </a-form-model-item>
      </a-form-model>
    </a-card>
  </page-header-wrapper>
</template>

<script>
import jushuitanApi from '@/api/jushuitan'
export default {
  data() {
    return {
      callbackLink: '',
      form: {
        app_Secret: '',
        app_Key: '',
      },
      rules: {
        app_Secret: [{ required: true, message: '请输入app_Secret', trigger: 'blur' }],
        app_Key: [{ required: true, message: '请输入app_Key', trigger: 'blur' }],
      },
      modalLoading: false,
    }
  },

  methods: {
    async confSave(conf) {
      this.modalLoading = true

      const res = await jushuitanApi
        .confSave({
          conf: conf,
        })
        .catch((err) => {
          this.$message.error(err.message)
          this.modalLoading = false
        })
      this.modalLoading = false
      if (res.code !== 0) {
        this.$error({
          content: res.msg,
        })
      } else {
        this.getData()
        this.$message.success(res.msg)
        this.visible = false
      }
    },

    // 表单提交
    submitForm() {
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          this.confSave(this.form)
        }
      })
    },

    async getData() {
      const res = await jushuitanApi.conf()
      if (res.code == 0) {
        let { app_Key, app_Secret } = res.data
        this.form = {
          app_Key: app_Key,
          app_Secret: app_Secret,
        }
        this.callbackLink = res.callbackLink
      }
    },

    onCopyText(text) {
      this.$copyText(text)
        .then((e) => {
          this.$message.success('复制成功')
        })
        .catch((err) => {
          console.log(err)
          this.$message.error('复制失败')
        })
    },
  },

  created() {
    this.getData()
  },
}
</script>

<style lang="less" scoped>
.title {
  color: #000;
  font-size: 16px;
  margin-bottom: 10px;
}
.tip {
  font-size: 14px;
  color: #8c8c8c;
  display: block;
}
</style>
